package com.lwc.cfdns.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lwc.cfdns.pojo.bo.monitor.OperLogBO;
import com.lwc.cfdns.pojo.entity.SystemLogininfor;
import com.lwc.cfdns.pojo.entity.SystemOperLog;
import com.lwc.cfdns.service.SystemOperLogService;
import com.lwc.cfdns.mapper.SystemOperLogMapper;
import com.lwc.cfdns.utils.ConvertUtils;
import com.lwc.cfdns.utils.StringUtils;
import com.lwc.cfdns.utils.ip.AddressUtils;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @author spring
 * @description 针对表【cfdns_system_oper_log(操作日志记录)】的数据库操作Service实现
 * @createDate 2023-08-02 16:17:15
 */
@Service
public class SystemOperLogServiceImpl extends ServiceImpl<SystemOperLogMapper, SystemOperLog>
        implements SystemOperLogService {

    /**
     * 创建
     *
     * @param systemOperLog
     * @return void
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/8/2 16:20:34
     */
    @Override
    @Async("threadPoolTaskExecutor")// 提交到线程池中去处理
    public void create(SystemOperLog systemOperLog) {
        if (StringUtils.isNotEmpty(systemOperLog.getOperIp())) {
            // 获取操作人所在地区
            systemOperLog.setOperLocation((String) AddressUtils.getRealAddressByIP(systemOperLog.getOperIp()));
        }
        this.baseMapper.insert(systemOperLog);
    }

    /**
     * 列表
     *
     * @param operLogBO
     * @return IPage<SystemOperLog>
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/8/3 15:02:57
     */
    @Override
    public IPage<SystemOperLog> lists(OperLogBO operLogBO) {
        Page<SystemOperLog> page = this.baseMapper.selectPage(operLogBO.toPage(), operLogBO.toQueryWrapper());
        page.convert(s -> {
            return s;
        });
        return page;
    }

    /**
     * 删除
     *
     * @param ids
     * @return Map<String, Object>
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/8/3 15:04:40
     */
    @Override
    public Map<String, Object> delete(String ids) {
        int i;
        if (ids.contains(",")) {
            i = this.baseMapper.delete(new LambdaQueryWrapper<SystemOperLog>().in(SystemOperLog::getId, ConvertUtils.toLongArray(ids)));
        } else {
            i = this.baseMapper.deleteById(ConvertUtils.toLong(ids));
        }
        Map<String, Object> map = new HashMap<>();
        map.put("result", i);
        return map;
    }

    /**
     * 清空
     *
     * @param
     * @return Map<String, Object>
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/8/3 15:05:32
     */
    @Override
    public Map<String, Object> clear() {
        int i = this.baseMapper.delete(new LambdaQueryWrapper<SystemOperLog>().eq(SystemOperLog::getDeleted, 0));
        Map<String, Object> map = new HashMap<>();
        map.put("result", i);
        return map;
    }
}




